package com.wman;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.Keyframe;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.TypeEvaluator;
import android.graphics.Point;
import android.os.Bundle;
import android.view.animation.DecelerateInterpolator;
import android.widget.ImageView;

import com.wman.sheep.common.utils.DensityUtil;
import com.wman.sheep.common.utils.ToastUtil;
import com.wman.utils.CityUtil;
import com.wman.view.CameraView;
import com.wman.view.CircleView;
import com.wman.view.CityView;
import com.wman.view.PointView;

import java.util.List;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private ImageView mViewProperty, mImageView, mImageView1;
    private CircleView mCircleView;
    private CameraView mCameraView;
    private CameraView mCameraView2;
    private PointView mPointView;
    private CityView mCityView;
    private List<String> mCities = CityUtil.getCities();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mViewProperty = findViewById(R.id.view_property);
        mImageView = findViewById(R.id.image);
        mImageView1 = findViewById(R.id.image1);
        mCircleView = findViewById(R.id.circle_view);
        mCameraView = findViewById(R.id.camera_view);
        mCameraView2 = findViewById(R.id.camera_view2);
        mPointView = findViewById(R.id.point_view);
        mCityView = findViewById(R.id.province_view);

        mViewProperty.animate()
                .setDuration(1000)
                .rotation(180)
                .alpha(0.5f)
                .translationX(DensityUtil.dip2px(200));
        //本质就是调用View的方法 mImageView.setTranslationX(0 ~ 200dp);

        //所有的属性都可以设置
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(mCircleView, "radius", DensityUtil.dip2px(100));
        objectAnimator.setStartDelay(1000);
        objectAnimator.start();

        ObjectAnimator bottomFlip = ObjectAnimator.ofFloat(mCameraView, "bottomFlip", 45);
        bottomFlip.setDuration(1500);

        ObjectAnimator flipRotation = ObjectAnimator.ofFloat(mCameraView, "flipRotation", 270);
        flipRotation.setDuration(1500);

        ObjectAnimator topFlip = ObjectAnimator.ofFloat(mCameraView, "topFlip", -45);
        topFlip.setDuration(1500);

        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.playSequentially(bottomFlip, flipRotation, topFlip);
        animatorSet.setStartDelay(1000);
        animatorSet.start();

        //同时改变，多个同时作用
        PropertyValuesHolder holder1 = PropertyValuesHolder.ofFloat("bottomFlip", 45);
        PropertyValuesHolder holder2 = PropertyValuesHolder.ofFloat("flipRotation", 270);
        PropertyValuesHolder holder3 = PropertyValuesHolder.ofFloat("topFlip", -45);
        ObjectAnimator objectAnimator1 = ObjectAnimator.ofPropertyValuesHolder(mCameraView2, holder1, holder2, holder3);
        objectAnimator1.setDuration(2000);
        objectAnimator1.setStartDelay(1000);
        objectAnimator1.start();

        float length = DensityUtil.dip2px(200);
        Keyframe keyframe1 = Keyframe.ofFloat(0, 0);
        Keyframe keyframe2 = Keyframe.ofFloat(0.2f, 1.2f * length);
        Keyframe keyframe3 = Keyframe.ofFloat(0.8f, 0.6f * length);
        Keyframe keyframe4 = Keyframe.ofFloat(1f, 1 * length);
        PropertyValuesHolder propertyValuesHolder = PropertyValuesHolder.ofKeyframe("translationX", keyframe1, keyframe2, keyframe3, keyframe4);
        ObjectAnimator objectAnimator2 = ObjectAnimator.ofPropertyValuesHolder(mImageView, propertyValuesHolder);
//        ObjectAnimator objectAnimator2 = ObjectAnimator.ofFloat(mImageView, "translationX", DensityUtil.dip2px(200));
        objectAnimator2.setDuration(2000);
        objectAnimator2.setStartDelay(1000);
        objectAnimator2.start();

        //入场（减速） DecelerateInterpolator
        //出场（加速） AccelerateInterpolator
        mImageView1.setTranslationX(DensityUtil.dip2px(-400));
        mImageView1.animate().translationX(DensityUtil.dip2px(200))
                .setStartDelay(1000)
                .setDuration(400)
                .setInterpolator(new DecelerateInterpolator())
                .start();

        Point target = new Point(DensityUtil.dip2px(300), DensityUtil.dip2px(60));
        ObjectAnimator objectAnimator3 = ObjectAnimator.ofObject(mPointView, "point", new PointEvaluator(), target);
        objectAnimator3.setDuration(2000);
        objectAnimator3.setStartDelay(2000);
        objectAnimator3.start();

        ObjectAnimator objectAnimator4 = ObjectAnimator.ofObject(mCityView, "city", new CityEvaluator(), "无锡");
        objectAnimator4.setDuration(5000);
        objectAnimator4.setStartDelay(2000);
        objectAnimator4.start();
        objectAnimator4.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                ToastUtil.showTextToast("End");
            }

            @Override
            public void onAnimationStart(Animator animation) {
                super.onAnimationStart(animation);
                ToastUtil.showTextToast("Start");
            }
        });
    }

    private class PointEvaluator implements TypeEvaluator<Point> {
        @Override
        public Point evaluate(float v, Point point, Point t1) {
            //初始值是(1,1)  目标值是(5,5)  fraction是0.2  x:1+(5-1)*0.2 y:1+(5-1)*0.2
            float x = point.x + (t1.x - point.x) * v;
            float y = point.y + (t1.y - point.y) * v;
            return new Point((int) x, (int) y);
        }
    }

    private class CityEvaluator implements TypeEvaluator<String> {
        @Override
        public String evaluate(float v, String s, String t1) {

            int startIndex = mCities.indexOf(s);
            int endIndex = mCities.indexOf(t1);
            int index = (int) (startIndex + (endIndex - startIndex) * v);
            return mCities.get(index);
        }
    }
}
